package com.szp.lg.blog.config;

import com.alibaba.fastjson.JSON;
import com.szp.lg.blog.config.props.DataSourceProps;
import com.szp.lg.blog.constant.ConstantDbZkPath;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

//@Configuration
public class DataSourceConfig1 implements InitializingBean, ApplicationContextAware {
//    @Autowired
//    CuratorFramework curatorFramework;
//    private ApplicationContext applicationContext;

//    @Autowired
//    JdbcTemplate jdbcTemplate;

//    @Autowired
//    DataSourceTransactionManager dataSourceTransactionManager;
//    @Autowired
//    PlatformTransactionManager platformTransactionManager;




    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//        this.applicationContext = applicationContext;
//        Properties props = new Properties();
//        props.setProperty( "dataSourceClassName" , "com.mysql.cj.jdbc.Driver" );
//                props.setProperty( "dataSource.user" , "root" );
//                props.setProperty( "dataSource.password" , "Mysql+1123" );
//                props.setProperty("")

    }



    @Override
    public void afterPropertiesSet() throws Exception {
//        //TODO 为数据库节点添加监听，当数据发生变化时，改变数据源的信息；
//        final NodeCache nodeCache = new NodeCache(curatorFramework, ConstantDbZkPath.DB_ZK_PATH, false);
//        nodeCache.getListenable().addListener(() -> {
//            System.out.println(ConstantDbZkPath.DB_ZK_PATH + "节点数据发生变化：" + nodeCache.getCurrentData());
//            ChildData childData = nodeCache.getCurrentData();
//            String db = new String(childData.getData());
//            System.out.println("//the new db:" + db);
//            changeDb(db);
//        });
//        //如果为true则首次不会缓存节点内容到cache中，默认为false,设置为true首次不会触发监听事件
//        try {
//            nodeCache.start(true);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

//    private void changeDb(String db) {
//        DataSourceProps dataSourceProps = JSON.parseObject(db, DataSourceProps.class);
//
//        HikariDataSource oldHikariDataSource = applicationContext.getBean(HikariDataSource.class);
//        oldHikariDataSource.close();
//        HikariDataSource newHikariDataSource = new HikariDataSource();
//        newHikariDataSource.setJdbcUrl(dataSourceProps.getUrl());
//        newHikariDataSource.setUsername(dataSourceProps.getUsername());
//        newHikariDataSource.setPassword(dataSourceProps.getPassword());
//
////        jdbcTemplate.setDataSource(newHikariDataSource);
////        JpaTransactionManager jpaTransactionManager = (JpaTransactionManager)platformTransactionManager;
////        jpaTransactionManager.setDataSource(newHikariDataSource);
//    }
}
